package com.cgah

import grails.test.*

class CustomerControllerUnitTests extends GrailsUnitTestCase {

    def session
    def params
    def redirectParams
    def flash

    /** Setup metaclass fixtures for mocking.  */
    void setUp() {
        super.setUp()
        session = [:]
        CustomerController.metaClass.getSession = {-> session }

        params = [id:1]
        CustomerController.metaClass.getParams = {-> params }

        redirectParams = [:]
        CustomerController.metaClass.redirect = { Map args -> redirectParams = args  }

        flash = [:]
        CustomerController.metaClass.getFlash = {-> flash }

        def logger = new Expando(debug: { println it }, info: { println it },
                warn: { println it }, error: { println it })
        CustomerController.metaClass.getLog = {-> logger }
    }

    protected void tearDown() {
        flash = null;
        redirectParams = null;
        params = null;
        session = null;
        super.tearDown()
    }

	/**
     * When trying to delete a customer fails, the error message must be put in flash scope var
	 */
    void testMessageInFlashWhenCustomerHasBidAndOrListing() {

        def customerServiceMock =  mockFor(CustomerService, false);
        customerServiceMock.demand.deleteCustomerById() { def id ->  return false}

        def customerController = new CustomerController()
        customerController.customerService = customerServiceMock.createMock()
        customerController.delete()

        assertTrue(flash.message.toString().contains("Delete Failed"));
    }


}
